<?php
/*
 * 手机客户端专场-竞价/代理出价接口
 * 2014-05-29
 */
include "../inc/public_header.php";
include "../func/user.php";				//用户信息处理函数
include "../func/bid.php";				//拍卖信息处理函数

//常量
$aid 		= $_REQUEST["aid"];		
$uuid 		= $_REQUEST["uuid"];	
$myPrice 	= $_REQUEST["myPrice"];	
$key 		= $_REQUEST["key"];	

//用户出价
$rs=array("success"=>"0");
if (isset($aid) and isset($uuid) and isset($myPrice) and isset($key)){
	$newkey = md5($aid.$uuid.$myPrice.'lhv5');
	if ($newkey!=$key){
		//KEY不正确
		$rs=array("success"=>"0","errCode"=>"-1","m"=>"k");
	}else{
		//是否存在currentprice
		$sql = "select lastprice from auction where aid = '".$aid."'";
		$db->query($sql);
		$isOrigin = "1";
		if($db->next_record()){
			$currentprice = $db->f("lastprice");
			if ($db->f("lastprice")){
				$isOrigin = "0";
			}
		}else{
			$currentprice = 0;
		}
		//没找到拍品记录，当前价格为0
		$rs=array("success"=>"0","price"=>$currentprice,"errCode"=>"-1","msg"=>"预制返回值");
		if (strlen($uuid) < 32){
			//"msg":"错误的ID"
			$rs=array("success"=>"0","price"=>$currentprice,"msg"=>"错误的ID");
		}else{
			//根据uuid查找ID
			$uInfoArr = user_getInfoFromUUID();
			if(!$uInfoArr){
				$rs=array("success"=>"0","price"=>$currentprice,"msg"=>"获取用户失败");
			}else{
				$uid = $uInfoArr["uid"];//用户信息主键
				$mobile = $uInfoArr["mobile"];
				//拍卖额度
				$userCreditLimit=userCreditLimit($uid);
				//当前最高代理价信息 
				$sql = "SELECT bidproxy,uid,is_bid from auction_userbidproxy where aid='".$aid."' order by xid desc limit 1";
				$db->query($sql);
				if($db->next_record()){
					$isbidproxy_bidproxy = $db->f("bidproxy");
					$isbidproxy_uid = $db->f("uid");
					$isbidproxy_is_bid = $db->f("is_bid");
				}
				//是否最高出价人
				$sql = "select bid,uid from auction_userlastbid where aid = '".$aid."' and is_bid = 1";
				$db->query($sql);
				if($db->next_record()){
					$islastbid_bid = $db->f("bid");
					$islastbid_uid = $db->f("uid");
				}
				//此处为客户端计算调整
				$fixPrice = 0;
				if($isbidproxy_is_bid == 1 and $isbidproxy_uid == $uid){
					$fixPrice = $isbidproxy_bidproxy;
				}else if($islastbid_uid == $uid){
					$fixPrice = $islastbid_bid;
				}
				if ($myPrice > ($userCreditLimit+$fixPrice)){
					//"msg":"额度不足"
					$rs=array("success"=>"0","price"=>$currentprice,"isOrigin"=>$isOrigin,"errCode"=>"-1","msg"=>"额度不足");
				}else{
					//获取当前价  atag 1竞拍中
					$sql = "select lastprice,starttime,endtime,bprice from auction where aid = '".$aid."' and is_delete = 0 and atag = 1";
					$db->query($sql);
					if($db->next_record()){
						$getlastprice_lastprice = $db->f("lastprice");
						$getlastprice_starttime = $db->f("starttime");
						$getlastprice_endtime = $db->f("endtime");
						$getlastprice_bprice = $db->f("bprice");
					}else{
						//"msg":"未找到拍品"
						$rs=array("success"=>"0","price"=>$currentprice,"isOrigin"=>$isOrigin,"errCode"=>"-1","msg"=>"未找到拍品");
					}
					if ($getlastprice_starttime > date('Y-m-d H:i:s',time()) or $getlastprice_endtime< date('Y-m-d H:i:s',time())){
						$rs=array("success"=>"0","price"=>$currentprice,"isOrigin"=>$isOrigin,"errCode"=>"-1","msg"=>"不在竞拍时间段");
					}else{
						//最小出价
						$firstPrice = $getlastprice_lastprice + addpfunc($getlastprice_lastprice);
						//有人出价?
						$sql = "select bid from auction_userlastbid where aid = '".$aid."'";
						$db->query($sql);
						if($db->next_record()){
							$isbid_bid = $db->f("bid");
						}
						//是否领先
						$sql = "select uid,is_bid from auction_userlastbid where aid = '".$aid."' AND uid = '".$uid."'";
						$db->query($sql);
						if($db->next_record()){
							$uidisLead_uid = $db->f("uid");
							$uidisLead_is_bid = $db->f("is_bid");
						}
						//代理价
						$sql = "SELECT bidproxy,uid from auction_userbidproxy where aid = '".$aid."' and uid='".$uid."' order by xid desc limit 1";
						$db->query($sql);
						if($db->next_record()){
							$haveproxy_bidproxy = $db->f("bidproxy");
							$haveproxy_uid = $db->f("uid");
						}
						$isLead = "0";			//是否领先
						$isProxy = "0";		//是否为代理价
						$proxyPrice = 0;		//代理价格
						$credit = 0;			//出价额度
						//用户出价额度
						$userCreditLimit=userCreditLimit($uid);
						if ($myPrice < $firstPrice){
							//如果我出的价格小于当前价格
							if ($uidisLead_uid){
								$isLead = $uidisLead_is_bid;
							}
							if ($haveproxy_uid){
								$isProxy = "1";
								$proxyPrice = $haveproxy_bidproxy;
							}
							//"msg":"出价太少"
							$rs=array("success"=>"1","price"=>$currentprice,"isLead"=>$isLead,"isProxy"=>$isProxy,"proxyPrice"=>$proxyPrice,"credit"=>$userCreditLimit,"isOrigin"=>$isOrigin,"errCode"=>"-1","msg"=>"出价太少");
						}else{
							//我出的价格大于当前价格
							//我出的价格小于底价
							if ($myPrice < $getlastprice_bprice){
								$b = $myPrice;
                                //更新最高出价
								$currentprice = newbid_1($aid,$uid,$mobile,$b);
							//我出的价格大于等于底价
							}else{
								//是否有人又出了最高价
								if (!$isbid_bid){
                                    //目前没人出价
                                    $b = $firstPrice;
                                    //更新最高出价
                                    $currentprice = newbid($aid,$uid,$mobile,$b);
                                    //如果我出的价格仍然大于最新的价格
                                    if ($myPrice > $firstPrice){
                                        $ip = $myPrice;
                                        //更新最高代理价
                                        $currentproxy = newbidproxy($aid,$uid,$getlastprice_endtime,$ip);
                                    }
                                }else{
									//我出的就是当前最高价
                                    if ($islastbid_uid == $uid){
                                        //是否设代理
                                        if (!$isbidproxy_uid){
                                            //没设置过代理
                                            $ip = $myPrice;
                                            $currentproxy = newbidproxy($aid,$uid,$getlastprice_endtime,$ip);
                                        }else{
                                            //设置了代理
                                            $up = $myPrice;
                                            $currentproxy = modbidproxy($aid,$uid,$getlastprice_endtime,$up);
                                        }									
                                    }else{
                                        //有人出了最高价，但不是我
                                        //是否设代理
                                        if(!$isbidproxy_uid){
                                            //没有设置代理
                                            $b = $firstPrice;
                                            $currentprice = newbid($aid,$uid,$mobile,$b);
                                            if ($myPrice > $firstPrice){
                                                $ip = $myPrice;
                                                $currentproxy = newbidproxy($aid,$uid,$getlastprice_endtime,$ip);
                                            }
                                        }else{
                                            //有人设置了代理
                                            $omobile = getuidmobile($isbidproxy_uid);
                                            //代理价小于我出的价格
                                            if ($isbidproxy_bidproxy < $myPrice){
                                                //旧代理人出价
                                                $ob = $isbidproxy_bidproxy;
                                                  
                                                //我出的价格超过旧代理人出的代理价
                                                //需要增加一条与旧代理人代理价相同的竞价记录
                                                //然后被旧代理人出最高的代理价超过
                                                //最后我再出最高的价格
                                                $currentprice = newbid($aid,$uid,$mobile,$ob);
                                                delproxy($aid);
  
                                                //wirtedump(omobile);
                                                $currentprice = newbid($aid,$isbidproxy_uid,$omobile,$ob);
                                                delproxy($aid);
                                                //delproxy = 1;
                                                $b = $isbidproxy_bidproxy + addpfunc($isbidproxy_bidproxy);
                                                $currentprice = newbid($aid,$uid,$mobile,$b);
                                                if ($myPrice > $b){
                                                    $ip = $myPrice;
                                                    $currentproxy = newbidproxy($aid,$uid,$getlastprice_endtime,$ip);
                                                }
                                            } else {
                                                //代理价大于当前出价
                                                $b = $myPrice;
                                                $currentprice = newbid($aid,$uid,$mobile,$b);
                                                $ob = $myPrice;
                                                $currentprice = newbid($aid,$isbidproxy_uid,$omobile,$ob);
                                                if ($isbidproxy_bidproxy == $myPrice){
                                                    //delproxy = 1;
                                                    delproxy($aid);
                                                }
                                            }
                                        }											
                                    }
								}
							}
							//再次检测
							//是否领先
							$sql = "select uid,is_bid from auction_userlastbid where aid = '".$aid."' AND uid = '".$uid."'";
							$db->query($sql);
							if($db->next_record()){
								$uidisLead2_is_bid = $db->f("is_bid");
								$uidisLead2_uid = $db->f("uid");
							}
							//最新代理价
							$sql = "SELECT bidproxy,uid from auction_userbidproxy where aid = '".$aid."' and uid='".$uid."' and limittime > now()";
							$db->query($sql);
							if($db->next_record()){
								$haveproxy2_bidproxy = $db->f("bidproxy");
								$haveproxy2_uid = $db->f("uid");
							}
							if ($uidisLead2_uid){
								$isLead = $uidisLead2_is_bid;
							}
							if ($haveproxy2_uid){
								$isProxy = "1";
								$proxyPrice = $haveproxy2_bidproxy;
							}
							if ($isLead == "0" AND $myPrice < $getlastprice_bprice){
								$msg = "当前出价没有超过该拍品的底价！";
							}else if ($isLead == "0" AND $myPrice >= $getlastprice_bprice){
								$msg = "未领先，有用户设置了更高代理价！";
							}else{
								$msg = "竞价成功！";
							}
							$userCreditLimit=userCreditLimit($uid);
							if($isLead == "1"){
								//在客户端中，可用额度减去了当前价，或者代理价（不该减去），在此加回来，避免客户端显示错误
								if($isProxy == "1"){
									$userCreditLimit = $userCreditLimit+$proxyPrice;
								}else{
									$userCreditLimit = $userCreditLimit+$currentprice;
								}
							}
							$rs=array("success"=>"1","price"=>$currentprice,"isLead"=>$isLead,"isProxy"=>$isProxy,"proxyPrice"=>$proxyPrice,"credit"=>$userCreditLimit,"isOrigin"=>"0","msg"=>$msg);
							//拍品代理价所有人
						}
					}
				}
			}
		}
	}

}else{
	//参数不足
	$rs=array("success"=>"0","errCode"=>"-1","msg"=>"参数不足");
}
echo json_encode($rs);

?>
